local doro__mashu = fk.CreateSkill{
  name = "doro__mashu",
}

doro__mashu:addEffect(fk.RoundEnd,{
    anim_type = "support",
    mute = true,
    can_trigger = function(self, event, target, player, data)
        if  player:hasSkill(doro__mashu.name) then
          local x = 0
          player.room.logic:getActualDamageEvents(1, function(e)
            if e.data.from ~= nil then
              x = x + e.data.damage
            end
            return false
          end, Player.HistoryRound)
          if x  > 0 then
            event:setCostData(self, x)
            return true
          end
        end
    end,
    on_cost = function (self, event, target, player, data)
        return player.room:askToSkillInvoke(player, {
          skill_name = doro__mashu.name,
          prompt = "你可以为场上玩家分配共计"..event:getCostData(self).."点普通伤害"
        })
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local max = event:getCostData(self)
        local dam = {}
        local tars = table.filter(room.alive_players,Util.IdMapper)
        while max > 0 and #tars > 0 do
          local tar = room:askToChoosePlayers(player,{
            targets = tars,
            min_num = 1,
            max_num = 1,
            prompt = "选择要被分配伤害的目标",
            skill_name = doro__mashu.name,
            cancelable = true,
            no_indicate = true,
          })
          if #tar > 0 then
            table.removeOne(tars,tar[1])
            local number
            local choices = {}
            for i = 0, max do
              table.insert(choices, tostring(i))
            end
            number = tonumber(room:askToChoice(player, {
              choices = choices,
              skill_name = doro__mashu.name
            })) ---@type integer
            table.insertIfNeed(dam,tar[1])
            room:setPlayerMark(tar[1],"@doro__mashu_damage-phase",number)
            max = max - number
          else
            break
          end
        end
        if #dam > 0 then
          room:delay(500)
          room:sortByAction(dam)
          for _, pid in ipairs(dam) do
            local p = pid
            if not p.dead then
              if player.general == "hxxishi__yaomeng"  or player.deputyGeneral == "hxxishi__yaomeng" then
                for i = 1,3 ,1 do
                  room:setEmotion(player, "./packages/huixiangkill/image/anim/doro__yaomeng")
                end
              else
                room:notifySkillInvoked(player,doro__mashu.name,"offensive")
              end
              room:doIndicate(player,{pid})
              if p:getMark("@doro__mashu_damage-phase") > 0 then
                player.room:damage({
                from = player,
                to = p,
                damage = p:getMark("@doro__mashu_damage-phase"),
                skillName = doro__mashu.name
              })
              end
            end
          end
          for _, p in ipairs(room.players) do
            room:setPlayerMark(p,"@doro__mashu_damage-phase", 0)
          end
        end
    end,
})

doro__mashu:addEffect(fk.Damage, {
    anim_type = "support",
    mute = true,
    can_trigger = function(self, event, target, player, data)
      if not player:hasSkill(doro__mashu.name) then return false end
      if event == fk.Damage then
        return target ~= player
      end
    end,
    on_cost = function (self, event, target, player, data)
      return player.room:askToSkillInvoke(player, {
        skill_name = doro__mashu.name,
        prompt = "你可以立即结束当前回合"
      })
    end,
    on_use = function(self, event, target, player, data)
      local room = player.room
      if event == fk.Damage then
        room:doIndicate(player,{room.current})
        if player.general == "hxxishi__yaomeng"  or player.deputyGeneral == "hxxishi__yaomeng" then
          for i = 1, 3 ,1 do
            room:setEmotion(player, "./packages/huixiangkill/image/anim/doro__yaomeng")
          end
        else
          room:notifySkillInvoked(player,self.name,"negative")
        end
        room.logic:breakTurn()
      end
    end,
})

return doro__mashu